package cc.cc4414.cc02.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.cc4414.cc02.core.aspect.LogAspect;
import cc.cc4414.cc02.core.controller.BaseController;
import cc.cc4414.cc02.core.result.ResultEnum;
import cc.cc4414.cc02.core.result.ResultVO;
import cc.cc4414.cc02.system.entity.Authority;
import cc.cc4414.cc02.system.entity.Resource;
import cc.cc4414.cc02.system.form.ResourceAuthorityForm;
import cc.cc4414.cc02.system.service.ResourceService;

/**
 * @author cc 2018年7月25日
 */
@Validated
@RestController
@RequestMapping("/resource")
public class ResourceController extends BaseController {

	@Autowired
	private ResourceService resourceService;

	/**
	 * 查询所有资源信息
	 * 
	 * @return 所有资源
	 */
	@GetMapping("all")
	public ResultVO<List<Resource>> all() {
		return new ResultVO<List<Resource>>(resourceService.findAll());
	}

	/**
	 * 通过资源id查找能访问该资源的所有权限
	 * 
	 * @param resourceId
	 *            资源id
	 * @return 能访问该资源的所有权限
	 */
	@GetMapping("findAuthority")
	public ResultVO<List<Authority>> findAuthority(@RequestParam Long resourceId) {
		return new ResultVO<List<Authority>>(resourceService.findAuthorityByResourceId(resourceId));
	}

	/**
	 * 新增资源
	 * 
	 * @param resource
	 *            新增的资源
	 * @return
	 */
	@PostMapping("add")
	@LogAspect("新增资源")
	public ResultVO<Object> add(@RequestBody Resource resource) {
		resourceService.save(resource);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}

	/**
	 * 通过资源id删除资源
	 * 
	 * @param id
	 *            资源id
	 * @return 是否删除成功
	 */
	@PostMapping("delete")
	@LogAspect("删除资源")
	public ResultVO<Object> delete(@RequestParam Long id) {
		resourceService.delete(id);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}

	/**
	 * 修改访问id为resourceId的资源的所需权限
	 * 
	 * @param resourceAuthority
	 *            资源id和能访问该资源的所有权限id
	 * @return 是否修改成功
	 */
	@PostMapping("updateResourceAuthority")
	@LogAspect("修改资源权限")
	public ResultVO<Object> updateResourceAuthority(@Validated @RequestBody ResourceAuthorityForm resourceAuthority) {
		resourceService.updateResourceAuthority(resourceAuthority.getResourceId(), resourceAuthority.getAuthorityIds());
		return new ResultVO<>(ResultEnum.SUCCESS);
	}

	/**
	 * 修改资源
	 * 
	 * @param resource
	 *            修改后的资源
	 * @return 是否修改成功
	 */
	@PostMapping("update")
	@LogAspect("修改资源")
	public ResultVO<Object> update(@RequestBody Resource resource) {
		resourceService.update(resource);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}
}
